// ActionScript file
import georah.carto.geometries.OGCPoint;
import georah.map.MapEvent;
import georah.map.MapMouseEvent;
import georah.map.layers.ImageLayerEvent;
import georah.map.tools.MapToolEvent;
import georah.map.tools.PointQueryEvent;

import mx.events.ItemClickEvent;
import mx.managers.CursorManager;

private var identifyWhere:OGCPoint = new OGCPoint();

private function initMouseDefaults():void
{
    mouseDefaults.addEventListener(MapToolEvent.ENABLE, function(event:MapToolEvent):void
        {
        //
        });

    mouseDefaults.addEventListener(MapToolEvent.DISABLE, function(event:MapToolEvent):void
        {
        //
        });
}

private function initMouseQuery():void
{
    usmap.acetate.addChild(identifyInfo);

    mouseQuery.addEventListener(MapToolEvent.ENABLE, function(event:MapToolEvent):void
        {
            usmap.layers.useHandCursor = true;
            usmap.layers.buttonMode = true;
            usmap.layers.mouseChildren = false;
        });

    mouseQuery.addEventListener(MapToolEvent.DISABLE, function(event:MapToolEvent):void
        {
            usmap.layers.mouseChildren = true;
            usmap.layers.buttonMode = false;
            usmap.layers.useHandCursor = false;
        });

    mouseQuery.addEventListener(PointQueryEvent.IDENTIFY, function(event:PointQueryEvent):void
        {
            var pixel:Point = usmap.fromWhereToLayersPixel(event.where);
            identifyInfo.x = pixel.x;
            identifyInfo.y = pixel.y;
            identifyWhere = event.where;
            identifyLabel.text = "LOADING...";
            identifyInfo.visible = true;
        });

    mouseQuery.addEventListener(PointQueryEvent.IDENTIFY_RESULT, function(event:PointQueryEvent):void
        {
            var name:String = "NO FEATURE HERE";
            try
            {
                name = String(event.result.split("\n")[3]).split(" = ")[1];
            }
            catch (e:*)
            {
            }
            identifyLabel.text = name;
            identifyInfo.visible = true;

            CursorManager.removeBusyCursor();
        });

    mouseQuery.addEventListener(PointQueryEvent.IDENTIFY_FAULT, function(event:PointQueryEvent):void
        {
            identifyLabel.text = event.result;

            CursorManager.removeBusyCursor();
        });

    usmap.addEventListener(MapEvent.CHANGE, function(event:MapEvent):void
        {
            var pixel:Point = usmap.fromWhereToLayersPixel(identifyWhere);
            identifyInfo.x = pixel.x;
            identifyInfo.y = pixel.y;
        });
}

private function init():void
{
    Security.allowDomain("*");
    Security.allowInsecureDomain("*");

    usmap.enableTool(keyboardDefaults);
    usmap.enableTool(mouseDefaults);
    usmap.enableTool(naviagtion);

    usmap.addLayer(baseLayer);
    usmap.addLayer(markerLayer);

    usmap.enableTool(keyboardDefaults);

    usmap.addEventListener(MapMouseEvent.DRAG_START, function(event:MapMouseEvent):void
        {
            usmap.layers.useHandCursor = true;
            usmap.layers.buttonMode = true;
            usmap.layers.mouseChildren = false;
        });

    usmap.addEventListener(MapMouseEvent.DRAG_STOP, function(event:MapMouseEvent):void
        {
            usmap.layers.mouseChildren = true;
            usmap.layers.buttonMode = false;
            usmap.layers.useHandCursor = false;
        });

    usmap.addEventListener(MapMouseEvent.MOUSE_MOVE, function(event:MapMouseEvent):void
        {
            infoLabel.text = event.where.x + ", " + event.where.y;
        });

    baseLayer.addEventListener(ImageLayerEvent.LOAD, function(event:ImageLayerEvent):void
        {
            CursorManager.setBusyCursor();
        });

    baseLayer.addEventListener(ImageLayerEvent.LOAD_FAULT, function(event:ImageLayerEvent):void
        {
            CursorManager.removeBusyCursor();
        });

    baseLayer.addEventListener(ImageLayerEvent.LOAD_RESULT, function(event:ImageLayerEvent):void
        {
            CursorManager.removeBusyCursor();
        });

    initMouseDefaults();
    initMouseQuery();
}

private function tools_clickHandler(event:ItemClickEvent):void
{
    switch (event.index)
    {
        case 0:
            usmap.enableTool(mouseDefaults);
            identifyInfo.visible = false;
            break;
        case 1:
            usmap.enableTool(dragZoom);
            break;
        case 2:
            usmap.enableTool(mouseQuery);
            break;
    }
}

private function commands_clickHandler(event:ItemClickEvent):void
{
    switch (event.index)
    {
        case 0:
            naviagtion.resetCamera();
            break;
        case 1:
            naviagtion.zoom(Navigation.ZOOM_IN);
            break;
        case 2:
            naviagtion.zoom(Navigation.ZOOM_OUT);
            break;
    }
}
